package de.graph.common.impl;

import java.util.Set;

import de.graph.common.Element;
import de.graph.document.Document;

abstract class ElementImpl implements Element {

    protected static final String LABEL = "label";
    
	protected final Document document;

	protected ElementImpl(final Document document) {
		this.document = document;
	}

    public String getLabel() {
        return getDocument().getProperty(LABEL, "");
    }

	@Override
	public <ValueType> ValueType getProperty(String key) {
		return document.getProperty(key);
	}

	@Override
	public <ValueType> void setProperty(String key, ValueType value) {
		document.setProperty(key, value);
	}

	@Override
	public <ValueType> ValueType removeProperty(String key) {
		return document.removeProperty(key);
	}

	@Override
	public Set<String> getPropertyKeys() {
		return document.getPropertyKeys();
	}

	protected Document getDocument() {
        return document;
    }
	
	@Override
	public int hashCode() {
		return getDocument().getId().hashCode();
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}

		if (obj == null) {
			return false;
		}

		if (!(obj instanceof ElementImpl)) {
			return false;
		}

		ElementImpl other = (ElementImpl) obj;

		return getDocument().getId().equals(other.getDocument().getId());
	}
}
